/*
 * @(#)UserRecordPermission.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.user.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserRecordPermission;

/**
  UserRecordPermissionDAObject.java

  This class is the data access bean for table "USER_RECORD_PERMISSION".

  @author      Angus Shiu
  @company     DCIVision Limited
  @creation date   16/07/2003
  @version     $Revision: 1.27.4.1 $
*/

public class UserRecordPermissionDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.27.4.1 $";

  public static final String TABLE_NAME = "USER_RECORD_PERMISSION";

  public UserRecordPermissionDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("USER_RECORD_ID");
    this.vecDBColumn.add("OBJECT_TYPE");
    this.vecDBColumn.add("OBJECT_ID");
    this.vecDBColumn.add("PERMISSION");
    this.vecDBColumn.add("MUST_FLAG");
    this.vecDBColumn.add("START_TIME");
    this.vecDBColumn.add("END_TIME");
    this.vecDBColumn.add("GRANTER_USER_RECORD_ID");
    this.vecDBColumn.add("CAN_GRANT");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          return(tmpUserRecordPermission);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          tmpUserRecordPermission.setRecordCount(totalNumOfRecord);
          tmpUserRecordPermission.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserRecordPermission tmpUserRecordPermission = (UserRecordPermission)((UserRecordPermission)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   USER_RECORD_PERMISSION(ID, USER_RECORD_ID, OBJECT_TYPE, OBJECT_ID, PERMISSION, MUST_FLAG, START_TIME, END_TIME, GRANTER_USER_RECORD_ID, CAN_GRANT, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpUserRecordPermission.getUserRecordID());
        setPrepareStatement(preStat, 3, tmpUserRecordPermission.getObjectType());
        setPrepareStatement(preStat, 4, tmpUserRecordPermission.getObjectID());
        setPrepareStatement(preStat, 5, tmpUserRecordPermission.getPermission());
        setPrepareStatement(preStat, 6, tmpUserRecordPermission.getMustFlag() ? GlobalConstant.TRUE : GlobalConstant.FALSE);
        setPrepareStatement(preStat, 7, tmpUserRecordPermission.getStartTime());
        setPrepareStatement(preStat, 8, tmpUserRecordPermission.getEndTime());
        setPrepareStatement(preStat, 9, tmpUserRecordPermission.getGranterUserRecordID());
        setPrepareStatement(preStat, 10, tmpUserRecordPermission.getCanGrant());
        setPrepareStatement(preStat, 11, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 12, new Integer(0));
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        setPrepareStatement(preStat, 15, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 16, currTime);
        preStat.executeUpdate();
        tmpUserRecordPermission.setID(nextID);
        tmpUserRecordPermission.setCreatorID(sessionContainer.getUserRecordID());
        tmpUserRecordPermission.setCreateDate(currTime);
        tmpUserRecordPermission.setUpdaterID(sessionContainer.getUserRecordID());
        tmpUserRecordPermission.setUpdateDate(currTime);
        tmpUserRecordPermission.setUpdateCount(new Integer(0));
        tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
        tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
        return(tmpUserRecordPermission);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserRecordPermission tmpUserRecordPermission = (UserRecordPermission)((UserRecordPermission)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE USER_RECORD_PERMISSION ");
        sqlStat.append("SET    USER_RECORD_ID=?, OBJECT_TYPE=?, OBJECT_ID=?, PERMISSION=?, MUST_FLAG=?, START_TIME=?, END_TIME=?, GRANTER_USER_RECORD_ID=?, CAN_GRANT=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUserRecordPermission.getUserRecordID());
        setPrepareStatement(preStat, 2, tmpUserRecordPermission.getObjectType());
        setPrepareStatement(preStat, 3, tmpUserRecordPermission.getObjectID());
        setPrepareStatement(preStat, 4, tmpUserRecordPermission.getPermission());
        setPrepareStatement(preStat, 5, tmpUserRecordPermission.getMustFlag() ? GlobalConstant.TRUE : GlobalConstant.FALSE);
        setPrepareStatement(preStat, 6, tmpUserRecordPermission.getStartTime());
        setPrepareStatement(preStat, 7, tmpUserRecordPermission.getEndTime());
        setPrepareStatement(preStat, 8, tmpUserRecordPermission.getGranterUserRecordID());
        setPrepareStatement(preStat, 9, tmpUserRecordPermission.getCanGrant());
        setPrepareStatement(preStat, 10, new Integer(tmpUserRecordPermission.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 12, currTime);
        setPrepareStatement(preStat, 13, tmpUserRecordPermission.getID());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpUserRecordPermission.setUpdaterID(sessionContainer.getUserRecordID());
          tmpUserRecordPermission.setUpdateDate(currTime);
          tmpUserRecordPermission.setUpdateCount(new Integer(tmpUserRecordPermission.getUpdateCount().intValue() + 1));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          return(tmpUserRecordPermission);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    UserRecordPermission tmpUserRecordPermission = (UserRecordPermission)((UserRecordPermission)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpUserRecordPermission.getID());
        setPrepareStatement(preStat, 2, tmpUserRecordPermission.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpUserRecordPermission);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    UserRecordPermission tmpUserRecordPermission = (UserRecordPermission)this.oldValue;

    if (tmpUserRecordPermission != null) {
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getUserRecordID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getObjectType()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getObjectID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getPermission()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getMustFlag() ? GlobalConstant.TRUE : GlobalConstant.FALSE));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getStartTime()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getEndTime()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getGranterUserRecordID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getCanGrant()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdateDate()));
    }

    tmpUserRecordPermission = (UserRecordPermission)obj;
    if (tmpUserRecordPermission != null) {
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getUserRecordID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getObjectType()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getObjectID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getPermission()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getMustFlag() ? GlobalConstant.TRUE : GlobalConstant.FALSE));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getStartTime()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getEndTime()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getGranterUserRecordID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getCanGrant()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpUserRecordPermission.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  public synchronized List getListByUserRecordID(Integer userRecordID) throws ApplicationException {
    return(this.getListByUserRecordIDObjectType(userRecordID, GlobalConstant.OBJECT_TYPE_ALL));
  }

  public synchronized List getListByUserRecordIDObjectType(Integer userRecordID, String objType) throws ApplicationException {
    return(this.getListByUserRecordIDObjectTypeObjectID(userRecordID, objType, null));
  }

  /**
   * Get Objects shared by others by userRecordID,objectType and userGroupID
   * @param userRecordID
   * @param objectType
   * @param userGroupID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByUserRecordIDObjectTypeUserGroupID(Integer userRecordID, String objectType, Integer userGroupID) throws ApplicationException {
    return(this.getListByUserRecordIDObjectTypeObjectIDUserGroupID(userRecordID, objectType, null, userGroupID));
  }

  public synchronized List getListByUserRecordIDCalendarIdStartEndDateForSchedule(Integer userRecordID, Timestamp startDate, Timestamp endDate)  throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ,CALENDAR_RECORD C ");
        sqlStat.append("WHERE  A.OBJECT_ID = C.ID AND A.USER_RECORD_ID = ? AND A.OBJECT_TYPE = 'C' ");
        
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
          if ( (startDate != null) && (endDate != null)) {
        		sqlStat.append(" AND ((C.DATETIME BETWEEN TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS')) OR (C.ENDTIME BETWEEN TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS')) )");
          } else if (startDate != null) {
            sqlStat.append(" AND (C.DATETIME > TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS'))");
          } else if (endDate != null) {
            sqlStat.append(" AND  (C.ENDTIME < TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS'))");
          }
        } else {
          if ( (startDate != null) && (endDate != null)) {
                  sqlStat.append(" AND ((C.DATETIME BETWEEN '" + startDate + "' AND '" + endDate + "') OR (C.ENDTIME BETWEEN '" + startDate + "' AND '" + endDate + "') OR (C.DATETIME < '" + startDate + "' AND C.ENDTIME > '" + endDate + "')) ");
          } else if (startDate != null) {
              sqlStat.append(" AND (C.DATETIME > '" + startDate + "')");
          } else if (endDate != null) {
              sqlStat.append(" AND  (C.ENDTIME < '" + endDate + "')");
          }
        }


        sqlStat.append(" UNION ");
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ,CALENDAR_RECORD C,CALENDAR_RECUR R ");
        sqlStat.append("WHERE  A.OBJECT_ID = C.ID AND A.USER_RECORD_ID = ? AND A.OBJECT_TYPE = 'C' AND C.ID = R.CALENDAR_ID");
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        
        this.setPrepareStatement(preStat, 1, userRecordID);
        this.setPrepareStatement(preStat, 2, userRecordID);

        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getListByUserRecordIDObjectTypeObjectID(Integer userRecordID, String objType, Integer objID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.USER_RECORD_ID = ? ");
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          sqlStat.append("AND A.OBJECT_TYPE=? ");
        }
        if (objID!=null) {
          sqlStat.append("AND A.OBJECT_ID=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, userRecordID);
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          setPrepareStatement(preStat, 2, objType);
        }
        if (objID!=null) {
          setPrepareStatement(preStat, 3, objID);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Retrieve the permission string (access control list) of a particular entity that a user has right to access
   * @param userRecordID	The user record to query for (mandatory)
   * @param objType	The entity object type (mandatory)
   * @param objID	The entity ID (mandatory)
   * @return	The permission string that the user can access on that entity, return empty string if that user record 
   *            has no access right on it.
   */  
  public synchronized String getPermissionByUserRecordIDObjectTypeObjectID(Integer userRecordID, String objType, Integer objID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    String permission = "";
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT PERMISSION ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION ");
        sqlStat.append("WHERE  USER_RECORD_ID = ? ");
        sqlStat.append("AND OBJECT_TYPE=? ");
        sqlStat.append("AND OBJECT_ID=? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, userRecordID);
        setPrepareStatement(preStat, 2, objType);
        setPrepareStatement(preStat, 3, objID);

        rs = preStat.executeQuery();
        if (rs.next()) {
          permission = getResultSetString(rs, 1);
        }        
        return permission;
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }  

  public synchronized List getListByObjectTypeObjectID(String objType, Integer objID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE=? AND A.OBJECT_ID=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objType);
        setPrepareStatement(preStat, 2, objID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getListByObjectTypeObjectIDUpdaterID(String objType, Integer objID, Integer updaterID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE=? AND A.OBJECT_ID=? AND UPDATER_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objType);
        setPrepareStatement(preStat, 2, objID);
        setPrepareStatement(preStat, 3, updaterID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized void deleteListByObjectTypeObjectID(String objectType, Integer objectID, List existIDList) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION ");
        sqlStat.append("WHERE  OBJECT_TYPE = ? AND OBJECT_ID = ? ");
        if (!Utility.isEmpty(existIDList)) {
          sqlStat.append("AND ID NOT IN (");
          sqlStat.append(existIDList.get(0));
          for (int i = 1; i < existIDList.size(); i++) {
            sqlStat.append(", " + existIDList.get(i));
          }
          sqlStat.append(") ");
        }

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objectType);
        setPrepareStatement(preStat, 2, objectID);

        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized void deleteListByObjectTypeObjectIDUpdaterID(String objectType, Integer objectID, Integer updaterID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION ");
        sqlStat.append("WHERE  OBJECT_TYPE = ? AND OBJECT_ID = ? AND UPDATER_ID = ? ");

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objectType);
        setPrepareStatement(preStat, 2, objectID);
        setPrepareStatement(preStat, 3, updaterID);
        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized void deleteListByObjectTypeUserRecordID(String objectType, Integer userRecordID, List existIDList) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION ");
        sqlStat.append("WHERE  OBJECT_TYPE = ? AND USER_RECORD_ID = ? ");
        if (!Utility.isEmpty(existIDList)) {
          sqlStat.append("AND ID NOT IN (");
          sqlStat.append(existIDList.get(0));
          for (int i = 1; i < existIDList.size(); i++) {
            sqlStat.append(", " + existIDList.get(i));
          }
          sqlStat.append(") ");
        }

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objectType);
        setPrepareStatement(preStat, 2, userRecordID);

        updateCnt = preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getListByUserRecordIDObjectTypeParentID(Integer userRecordID, String objType, Integer parentID) throws ApplicationException {
    //log.debug("getListByUserRecordIDObjectTypeParentID with userRecordID: " + userRecordID + " , objType: " + objType + " , parentID: " + parentID);
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    String joinTable = null;

    if (GlobalConstant.OBJECT_TYPE_FUNCTION.equals(objType)) {
      joinTable = "SYS_FUNCTION";
    } else if (GlobalConstant.OBJECT_TYPE_DOCUMENT.equals(objType)) {
      joinTable = "DMS_DOCUMENT";
    }

    synchronized(dbConn) {
      try {
        sqlStat.append(" SELECT URP.ID, URP.USER_RECORD_ID, URP.OBJECT_TYPE, URP.OBJECT_ID, URP.PERMISSION, URP.MUST_FLAG, URP.START_TIME, URP.END_TIME, URP.GRANTER_USER_RECORD_ID, URP.CAN_GRANT, URP.RECORD_STATUS, URP.UPDATE_COUNT, URP.CREATOR_ID, URP.CREATE_DATE, URP.UPDATER_ID, URP.UPDATE_DATE ");
        sqlStat.append(" FROM   USER_RECORD_PERMISSION URP LEFT JOIN " + joinTable + " SF ON URP.OBJECT_ID = SF.ID ");
        sqlStat.append(" WHERE  URP.USER_RECORD_ID = ? ");
        if (parentID!=null) {
          sqlStat.append("AND SF.PARENT_ID = ? ");
        }
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          sqlStat.append(" AND URP.OBJECT_TYPE=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int i=1;
        this.setPrepareStatement(preStat, i++, userRecordID);
        if (parentID!=null) {
          this.setPrepareStatement(preStat, i++, parentID);
        }
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          setPrepareStatement(preStat, i++, objType);
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getRequiredListByObjectTypeObjectID(String objType, Integer objID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE=? AND A.OBJECT_ID=? AND A.MUST_FLAG=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objType);
        setPrepareStatement(preStat, 2, objID);
        setPrepareStatement(preStat, 3, GlobalConstant.TRUE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * Get objects shared by others by userRecordID,objectType,objectID and userGroupID
   * @param userRecordID
   * @param objType
   * @param objID
   * @param userGroupID
   * @return
   * @throws ApplicationException
   */

  public synchronized List getListByUserRecordIDObjectTypeObjectIDUserGroupID(
      Integer userRecordID,
      String  objType,
      Integer objID,
      Integer userGroupID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A, MTM_USER_RECORD_USER_GROUP B ");
        sqlStat.append("WHERE  A.CREATOR_ID = B.USER_RECORD_ID AND A.RECORD_STATUS = B.RECORD_STATUS AND A.RECORD_STATUS = ? AND A.USER_RECORD_ID = ? AND B.USER_GROUP_ID = ? ");
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          sqlStat.append("AND A.OBJECT_TYPE=? ");
        }
        if (objID!=null) {
          sqlStat.append("AND OBJECT_ID=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, userRecordID);
        this.setPrepareStatement(preStat, 3, userGroupID);
        if (!GlobalConstant.OBJECT_TYPE_ALL.equals(objType)) {
          setPrepareStatement(preStat, 4, objType);
        }
        if (objID!=null) {
          setPrepareStatement(preStat, 5, objID);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getListByUserRecordIDUserGroupIDStartEndDateForSchedule(
      Integer userRecordID,
      Integer userGroupID,
      Timestamp startDate,
      Timestamp endDate) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A, MTM_USER_RECORD_USER_GROUP B, CALENDAR_RECORD C ");
        sqlStat.append("WHERE  A.CREATOR_ID = B.USER_RECORD_ID AND A.OBJECT_ID = C.ID AND A.RECORD_STATUS = B.RECORD_STATUS AND A.OBJECT_TYPE = 'C' AND A.RECORD_STATUS = ? AND A.USER_RECORD_ID = ? AND B.USER_GROUP_ID = ? ");
        
        if (DataSourceFactory.getDatabaseType() == DataSourceFactory.DB_ORACLE) {
          if ( (startDate != null) && (endDate != null)) {
        		sqlStat.append(" AND ((C.DATETIME BETWEEN TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS')) OR (C.ENDTIME BETWEEN TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS')) )");
          } else if (startDate != null) {
            sqlStat.append(" AND (C.DATETIME > TO_DATE('" + TextUtility.formatDate(startDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS'))");
          } else if (endDate != null) {
            sqlStat.append(" AND  (C.ENDTIME < TO_DATE('" + TextUtility.formatDate(endDate,"yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS'))");
          }
        } else {
          if ( (startDate != null) && (endDate != null)) {
                  sqlStat.append(" AND ((C.DATETIME BETWEEN '" + startDate + "' AND '" + endDate + "') OR (C.ENDTIME BETWEEN '" + startDate + "' AND '" + endDate + "') OR (C.DATETIME < '" + startDate + "' AND C.ENDTIME > '" + endDate + "')) ");
          } else if (startDate != null) {
              sqlStat.append(" AND (C.DATETIME > '" + startDate + "')");
          } else if (endDate != null) {
              sqlStat.append(" AND  (C.ENDTIME < '" + endDate + "')");
          }
        }

        sqlStat.append(" UNION ");
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A, MTM_USER_RECORD_USER_GROUP B, CALENDAR_RECORD C,CALENDAR_RECUR R ");
        sqlStat.append("WHERE  A.CREATOR_ID = B.USER_RECORD_ID AND A.OBJECT_ID = C.ID AND A.RECORD_STATUS = B.RECORD_STATUS AND A.OBJECT_TYPE = 'C' AND A.RECORD_STATUS = ? AND A.USER_RECORD_ID = ? AND B.USER_GROUP_ID = ? AND C.ID = R.CALENDAR_ID");
        
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, userRecordID);
        this.setPrepareStatement(preStat, 3, userGroupID);
        this.setPrepareStatement(preStat, 4, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 5, userRecordID);
        this.setPrepareStatement(preStat, 6, userGroupID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized List getListByObjectType(String objType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.USER_RECORD_ID, A.OBJECT_TYPE, A.OBJECT_ID, A.PERMISSION, A.MUST_FLAG, A.START_TIME, A.END_TIME, A.GRANTER_USER_RECORD_ID, A.CAN_GRANT, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   USER_RECORD_PERMISSION A ");
        sqlStat.append("WHERE  A.OBJECT_TYPE=?  ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, objType);
        rs = preStat.executeQuery();
        while (rs.next()) {
          UserRecordPermission tmpUserRecordPermission = new UserRecordPermission();
          tmpUserRecordPermission.setID(getResultSetInteger(rs, "ID"));
          tmpUserRecordPermission.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpUserRecordPermission.setObjectType(getResultSetString(rs, "OBJECT_TYPE"));
          tmpUserRecordPermission.setObjectID(getResultSetInteger(rs, "OBJECT_ID"));
          tmpUserRecordPermission.setPermission(getResultSetString(rs, "PERMISSION"));
          tmpUserRecordPermission.setMustFlag(GlobalConstant.TRUE.equals(getResultSetString(rs, "MUST_FLAG")));
          tmpUserRecordPermission.setStartTime(getResultSetTimestamp(rs, "START_TIME"));
          tmpUserRecordPermission.setEndTime(getResultSetTimestamp(rs, "END_TIME"));
          tmpUserRecordPermission.setGranterUserRecordID(getResultSetInteger(rs, "GRANTER_USER_RECORD_ID"));
          tmpUserRecordPermission.setCanGrant(getResultSetString(rs, "CAN_GRANT"));
          tmpUserRecordPermission.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpUserRecordPermission.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpUserRecordPermission.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpUserRecordPermission.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpUserRecordPermission.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpUserRecordPermission.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpUserRecordPermission.setCreatorName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getCreatorID()));
          tmpUserRecordPermission.setUpdaterName(UserInfoFactory.getUserFullName(tmpUserRecordPermission.getUpdaterID()));
          result.add(tmpUserRecordPermission);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
}
